package com.xinguan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinguan.entity.Admin;
import com.xinguan.mapper.AdminMapper;
import com.xinguan.pojo.dto.LoginDto;
import com.xinguan.service.AdminService;
import com.xinguan.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;


@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Autowired
    private AdminMapper adminMapper;


    @Override
    public void deleteAdmin(String id) {
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getId,id);
        Admin admin = adminMapper.selectOne(wrapper);
        admin.setId(id);
        adminMapper.deleteById(admin);
    }

    @Override
    public Map<String,Object> getAdmin(String id) {
        LambdaQueryWrapper<Admin> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getId,id);
        Admin admin = adminMapper.selectOne(wrapper);
        Map<String,Object> data = new HashMap<>();
        data.put("adminaccount",admin.getAdminaccount());
        data.put("name",admin.getName());
        return data;


    }

    @Override
    public Boolean isUsernameTaken(String username) {
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getAdminaccount, username);
        Admin existAdmin = adminMapper.selectOne(wrapper);
        return existAdmin!= null;
    }

    @Override
    public void registerUser(Admin user) {
        adminMapper.insert(user);
    }


//    @Override
//    public void updatePassword(ChangePasswordDto changePasswordDto) {
//         LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
//         wrapper.eq(Admin::getId,changePasswordDto.getId());
//        Admin admin = adminMapper.selectOne(wrapper);
//        String encrypt = MD5.encrypt(changePasswordDto.getModifiedpassword());
//        admin.setId(changePasswordDto.getId());
//        admin.setAdminpwd(encrypt);
//        adminMapper.updateById(admin);
//
//    }

    @Override
    public Map<String,Object> login(LoginDto loginDto) {
        Boolean isSuccess = true;

        //根据用户名和密码查询
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        String username = loginDto.getAdminaccount();
        wrapper.eq(Admin::getAdminaccount,username);
        Admin admin = adminMapper.selectOne(wrapper);
        if(admin==null){
            isSuccess = false;
        }

        //判断密码
        //获取输入的密码
        String password_input = MD5.encrypt(loginDto.getAdminpwd());

        if(!admin.getAdminpwd().equals(password_input)){
            isSuccess = false;
        }

        //使用JWT根据用户id和用户名称生成token字符串
        String token = JWTUtils.generateToken(admin.getId(), JWTUtils.EXPIRATION_TIME);

        //返回
        Map<String,Object> data =new HashMap<>();
        data.put("token", token);
        data.put("isSuccess", isSuccess);
        return  data;

    }
}



